//
//  SunLoginVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/17.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunLoginVC.h"
#import "SunLoginCheckVC.h"
#import "SunResignVC.h"

@interface SunLoginVC ()<UITextFieldDelegate>
{
    UIImageView *_big;
    UILabel *_bigLabel;
    UIButton *_nextBtn;
    UITextField *_phone;
}
@end

@implementation SunLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    self.title = @"登录/注册";
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)creatUI{
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = WHITE;
    [self.view addSubview:bgView];
    [bgView lt_Top:SunEdgesMake(Y(23), 0, 0, Y(74)) to:nil save:YES];
    UIImageView *phoneImg = [[UIImageView alloc]init];
    [bgView addSubview:phoneImg];
    phoneImg.image = [UIImage imageNamed:@"login_phone"];
    [phoneImg lt_YcenterTo:nil save:YES];
    [phoneImg lt_setLeft:X(18) to:nil save:YES];
    _phone = [[UITextField alloc]init];
    _phone.delegate = self;
    [bgView addSubview:_phone];
    [_phone lt_YcenterTo:nil save:YES];
    [_phone lt_setLeft:X(10) to:phoneImg save:YES];
    [_phone lt_setWidth:X(308) save:YES];
    _phone.placeholder = @"请输入您的手机号";
    _phone.textColor = BLACK;
    _phone.font = [UIFont systemFontOfSize:16];
    _phone.keyboardType = UIKeyboardTypeNumberPad;
    _phone.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phone.returnKeyType=UIReturnKeyDone;
    _big = [[UIImageView alloc]init];
    [self.view addSubview:_big];
    [_big lt_XcenterTo:nil save:YES];
    [_big lt_setTop:X(12) to:_phone save:YES];
    [_big lt_setSize:CGSizeMake(X(300), Y(64)) save:YES];
    _big.image = [UIImage imageNamed:@"login_magnifier"];
    _bigLabel = [[UILabel alloc]init];
    [_big addSubview:_bigLabel];
    [_bigLabel lt_XcenterTo:nil save:YES];
    [_bigLabel lt_layoutView:_bigLabel side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(6) save:YES];
    _bigLabel.textColor = WHITE;
    _bigLabel.textAlignment = NSTextAlignmentCenter;
    _bigLabel.font = [UIFont systemFontOfSize:28];
    _big.hidden = YES;
    _nextBtn = [[UIButton alloc]init];
    [self.view addSubview:_nextBtn];
    [_nextBtn lt_Top:SunEdgesMake(Y(102), X(15), X(15), Y(44)) to:bgView save:YES];
    _nextBtn.clipsToBounds = YES;
    [_nextBtn setBackgroundColor:BLUE forState:UIControlStateNormal];
    [_nextBtn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    _nextBtn.layer.cornerRadius = 4;
    [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextBtn setTitleColor:WHITE forState:UIControlStateNormal];
    _nextBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [_nextBtn addTarget:self action:@selector(checkPhone) forControlEvents:UIControlEventTouchUpInside];
    _nextBtn.enabled = NO;
    UILabel *text = [[UILabel alloc]init];
    [self.view addSubview:text];
    [text lt_setTop:Y(6) to:_nextBtn save:YES];
    [text lt_XcenterTo:nil save:YES];
    text.textColor = RED;
    text.text = [Sun getUser:LOGINTEXT];
    text.font = [UIFont systemFontOfSize:15];
    text.textAlignment = NSTextAlignmentCenter;
}

- (void)checkPhone{
    [[Sun shareSun]showActivity:self];
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CHECKPHONE];
    NSDictionary *dic = @{@"phone":_phone.text};
    [HttpRequest postURL:url withParam:dic success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        if ([[requestDic objectForKey:@"status"]intValue] == 0) {
            SunResignVC *vc = [[SunResignVC alloc]init];
            vc.phone = _phone.text;
            vc.time = 60;
            [[Sun shareSun]showMsg:@"验证码已发送"];
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            SunLoginCheckVC *loginCheck = [[SunLoginCheckVC alloc]init];
            loginCheck.isLogin = self.isLogin;
            loginCheck.phone = _phone.text;
            loginCheck.isWeb = self.isWeb;
            [self.navigationController pushViewController:loginCheck animated:YES];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *bigger = textField.text;
    if (string.length > 0 && textField.text.length < 11) {
        bigger = [textField.text stringByAppendingString:string];
    }else if (string.length <= 0 && textField.text.length > 0){
        bigger = [textField.text substringToIndex:textField.text.length - 1];
    }
    if (bigger.length > 0) {
        _big.hidden = NO;
    }else{
        _big.hidden = YES;
    }
    if ([Sun checkPhone:bigger]) {
        _nextBtn.enabled = YES;
    }else{
        _nextBtn.enabled = NO;
    }
    _bigLabel.text = [Sun formatStringToPhone:bigger];
    if (textField.text.length >= 11 && string.length > 0) {
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    _nextBtn.enabled = NO;
    _big.hidden = YES;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
